<template>
<!--  商品相册-->
  <el-card shadow="never" class="aui-card--fill">
    <div>
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item prop="productId">
          <el-input v-model="dataForm.productId" clearable  placeholder="请输入商品ID"></el-input>
        </el-form-item>
        <el-form-item>
          <ren-select v-model="dataForm.imgType" dict-type="img_type" placeholder="图片类型"></ren-select>
        </el-form-item>
        <el-form-item prop="productId">
          <ComSelectPage ref="newPageSelect" @selectPageChange="e=>dataForm.productId=e"
                         :getUrl="{
        url:'/api/product/page',
        method:'get',
        params:{page: 1,limit: 10,total: 0},
      }" clearable placeholder="请选择商品" :defaultValue="dataForm.productId" selectLabel="productName" selectValue="id"></ComSelectPage>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('api:productAlbum:save')" type="primary" @click="addOrUpdateHandle()">
            新增
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('api:productAlbum:delete')" type="danger" @click="deleteHandle()">
            删除
          </el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" stripe highlight-current-row @selection-change="dataListSelectionChangeHandle" @sort-change="dataListSortChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="productId" label="商品ID" header-align="center" align="center" width="">
          <template slot-scope="{}" slot="header">
            <!-- 表头名称 -->
            <span>商品信息</span>
            <el-tooltip class="item" effect="dark" placement="top">
              <i class="el-icon-question" style="font-size:16px;"></i>
              <!-- 悬停提示内容 -->
              <div slot="content">
                <div>点击下方名称查看详细信息</div>
              </div>
            </el-tooltip>
          </template>
          <template slot-scope="{row}">
            <div class="flex_columns" @click="showProductList(row['productDTO'])" v-if="row['productDTO']">
              <div class="flex_rows flex_center">
                <el-avatar size="large" :src="row['productDTO']['imgUrl']" v-if="row['productDTO']['imgUrl']"></el-avatar>
                <div class="flex_columns" style="margin-left: 10px;">
                  <div class="flex_rows" style="text-decoration: underline;cursor: pointer;">ID:{{row['productDTO']['id']}}</div>
                  <div class="flex_rows flex_center" style="text-decoration: underline;cursor: pointer;">
                    <div class="ellipsis1" style="max-width: 150px;">
                      {{row['productDTO']['productName']}}
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div v-if="!row.productDTO">{{row.productId}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="groupName" label="图片" header-align="center" align="center" width="">
          <template slot-scope="{row,$index}">
            <!-- 图片展示 -->
            <el-popover trigger="click" placement="top" popper-class="popper">
              <img v-if="row.imgUrl" :src="row.imgUrl">
              <el-image v-if="row.imgUrl" slot="reference" :src="row.imgUrl" style="width: 70px;height: 70px;">
                <div slot="error">
                  暂无图片
                </div>
              </el-image>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="showState" label="图片类型" header-align="center" align="center" sortable width="">
          <template slot-scope="{row,$index}">
            <div v-if="row.imgType === 1" size="small" type="success">
              {{ $getDictLabel("img_type", row.imgType) }}
            </div>
            <div v-else size="small" type="info">{{
                $getDictLabel("img_type", row.imgType)
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" header-align="center" align="center" sortable width=""></el-table-column>
        <el-table-column prop="createDate" label="创建日期" header-align="center" align="center"></el-table-column>
        <el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
          <template slot-scope="{row,$index}">
            <el-link v-if="$hasPermission('api:productAlbum:update')" type="primary"
                       @click="addOrUpdateHandle(row.id)">修改
            </el-link>
            <el-link v-if="$hasPermission('api:productAlbum:delete')" type="primary"
                       @click="deleteHandle(row.id)">删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
                     layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
                     @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <!--查看商品信息组件-->
      <com-product-list :visible.sync="showProduct" :content="content"></com-product-list>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './productAlbumAddOrUpdate.vue'
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/api/productAlbum/page',
        getDataListIsPage: true,
        createdIsNeed:false,
        deleteURL: '/api/productAlbum',
        deleteIsBatch: true
      },
      showProduct:false,                   //是否显示信息
      content:{},                          //信息内容
      dataForm: {
        id: '',
        imgUrl: '',                        //商品的图片路径或链接
        imgUrlList:[],                     //多图数据
        imgType: '',                       //图片类别字段,用于区分不同类型的图片（如详情页0轮播图、1细节图等）
        productId: '',                     //关联到所属商品的ID
        sort: '',                          //用来显示商品在列表中的顺序
      },
      timeList:[]                          //时间戳数组
    }
  },
  watch: {
    '$route':'getPath'
  },
  components: {
    AddOrUpdate
  },
  created() {
  },
  mounted() {
    if (this.$route.query.productId){
      this.mixinViewModuleOptions.createdIsNeed = false
      this.dataForm.productId = this.timeList.includes(this.$route.query.time)?'': this.$route.query.productId
      this.getDataList()//查询数据
    }else {
      this.mixinViewModuleOptions.createdIsNeed = true
      this.getDataList()//查询数据
    }
    this.timeList.push(this.$route.query.time)
  },
  methods: {
    //监听路由变化
    getPath(){
      if (this.$route.query.productId){
        this.mixinViewModuleOptions.createdIsNeed = false
        this.dataForm.productId = this.timeList.includes(this.$route.query.time)?'': this.$route.query.productId
        this.getDataList()//查询数据
      }else {
        this.mixinViewModuleOptions.createdIsNeed = true
        this.dataForm.productId = ''
        this.getDataList()//查询数据
      }
      this.timeList.push(this.$route.query.time)
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.dataForm.id = id
        this.$refs.addOrUpdate.dataForm.productId = this.dataForm.productId
        this.$refs.addOrUpdate.init()
      })
    },
    //显示商品信息
    showProductList(content){
      this.showProduct=true;
      this.content= content;
    },
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-image__inner {
  //height: 50px;
}
img {
  height: 400px;
}
</style>
